// pages/orderlist/orderlist.js
const app = getApp();
import http from '../../utils/http.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    page: 1,
    pageSize: 10,
    isLastPage: false,
    nowTimes: '',
    businessId: '',
    orderList: [],
    status: [],
    orderType: 'reserve',
    statusTotal: [0, 0, 0]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options,"option")
    if (options.type) {
      // [0] 已预约 [1, 4]已核销 [2, 3]已取消
      // 0.预约成功 1.使用成功、待评论 2.预约未使用 3.取消预约 4.已完成
      this.setData({
        orderType: options.type
      })
      wx.setNavigationBarTitle({
        title: "预约列表"
      })
    }
    this.setData({
      businessId: options.businessId,
      status: options.type === '0' ? [0] : options.type === '1' ? [1, 4] : [2, 3],
      active: Number(options.type)
    })
    this.getUserList()
    this.getUserStatsuNum()
  },
  onReachBottom(){
    let page = this.data.page
    if (!this.data.isLastPage) {
      page ++
      this.setData({
        page
      })
      this.getUserList()
    } else {
      this.setData({
        loadingType: -1
      })
      // console.log('没有数据')
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log(111)
    wx.showNavigationBarLoading()
    wx.showLoading()
    this.setData({
      page: 1,
      pageSize: this.data.pageSize,
      isLastPage: false,
      orderList: []
    }, () => {
      this.getUserList()
      setTimeout(function() {
        // 取消标题栏进度条效果
        wx.hideNavigationBarLoading()
        // 取消页面刷新动画
        wx.stopPullDownRefresh()
        wx.hideLoading()
      },1500)
    })
  },

  orderlistToArr(list) {
    var map = {}
    for (var i = 0; i <list.length; i++) {
      if (!list[i] instanceof Array) {
      // if(!list[i] instanceof Array || list.length < 2) {
        continue;
      }
      let name = list[i].appointmentDate
      if (name != undefined) {
        if (map[name] == undefined) {
          map[name] = []
        }
        map[name].push(list[i])
      }
    }
    if(map == undefined) {
      return []
    }
    let array = []
    for (let p in map) {
      array.push(map[p])
    }
    return array
  },

  getUserList(){
    console.log(11)
    let that = this
    let listArr = that.data.orderList
    let nowTime = new Date()
    let month = Number(nowTime.getMonth()+1) >= 10 ? Number(nowTime.getMonth() + 1) :'0' + Number(nowTime.getMonth()+1)
    let day = Number(nowTime.getDate()) > 10 ? nowTime.getDate() : '0' + (nowTime.getDate())
    let nowTimes = nowTime.getFullYear() + '.' + month + '.' + day
    wx.showLoading()
    http.post('/api/store/v1/appointment/reception', {
      page: that.data.page,
      pageSize: that.data.pageSize,
      businessId: that.data.businessId,
      status: that.data.status
    }).then(res => {
      wx.hideLoading()
      if (res.code === 200) {
        if (res.data.data && res.data.data.length > 0) {
          listArr = listArr.concat(that.orderlistToArr(res.data.data));
          that.setData({
            orderList: listArr,
            nowTimes: nowTimes,
            isLastPage: res.data.is_last_page
          })
        }
      } else {
        wx.showToast({
          title: list.message,
          icon:'none',
          duration:2000
        })
      }
    })
  },
  getUserStatsuNum() {
    const arr = [[0], [1, 4], [2, 3]]
    arr.forEach((v, index) => {
      http.post('/api/store/v1/appointment/reception', {
        page: 1,
        pageSize: 1,
        businessId: this.data.businessId,
        status: v
      }).then(res => {
        const total = this.data.statusTotal
        total[index] = res.data.total_count
        this.setData({
          statusTotal: total
        })
      })
    })
  },

  // 点击上面tab
  onChange(e) {
    const index = Number(e.currentTarget.dataset.index)
    // [0] 已预约 [1, 4]已核销 [2, 3]已取消
    this.setData({
      status: index === 0 ? [0] : index === 1 ? [1, 4] : [2, 3],
      page: 1,
      pageSize: this.data.pageSize,
      isLastPage: false,
      orderList: [],
      active: index
    })
    this.getUserList()
  },

  toPhone(e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.phone,
      success(){},
      fail(){}
    })
  }
})